package com.universe.demo.springboot.controller;

import com.universe.demo.springboot.aop.AopService;
import com.universe.demo.springboot.entity.Person;
import com.universe.demo.springboot.entity.Student;
import groovy.lang.Binding;
import groovy.lang.GroovyShell;
import groovy.lang.Script;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by jsliu on 2019/1/13.
 */
@RestController
public class AopController {
    @Autowired
    private AopService aopService;
    @RequestMapping("/aop")
    public Object jdbc() {
        String script = "def eval = {if(it != null && it != ''){it.length()}else{ return null;}} \r\n eval(student.name)\r\n println student.metaClass;println com.universe.demo.springboot.entity.Student.metaClass";

        Map<String, Object> map = new HashMap<String, Object>();
        Student student = new Student();
        student.setName("abc");
        map.put("student", student);
        eval(script, map);
        return null;
    }

    public static Object eval(String script, Map<String, Object> param) {
        GroovyShell groovyShell = new GroovyShell();
        Script s = groovyShell.parse(script);
        s.setBinding(new Binding(param));
        return s.run();
    }

    @RequestMapping("/aop/test")
    public Object test() {
        Person person = new Person();
        person.setAddress("shanghai");
         aopService.test(person,"hello",123);
         return null;
    }
}
